package yhao.infra.feature.solr.local.core.facet;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;

import java.util.List;

/**
 * 查询条件
 */
public class FacetQueryInfo {
    private List<SolrFacetBean> facetFields;
    private FacetQueryParam param;

    /**
     * @param facetFields 面查询的字段
     */
    public FacetQueryInfo(List<SolrFacetBean> facetFields, FacetQueryParam param) {
        this.facetFields = facetFields;
        this.param = param==null?new FacetQueryParam():param;
    }

    public void makeFacetIndex(SolrQuery solrQuery) {
        solrQuery.setFacet(true);
        for (SolrFacetBean facetField : facetFields) {
            solrQuery.addFacetField(facetField.getFacetField());
            String fq = facetField.createFacetQuery();
            if (StringUtils.isNotEmpty(fq)) {
                solrQuery.addFacetQuery(facetField.createFacetQuery());
            }
        }
        solrQuery.setFacetMinCount(param.getMinCount());
        if(param.getLimit()!=null){
            solrQuery.setFacetLimit(param.getLimit());
        }
    }

    public FacetQueryParam getParam() {
        return param;
    }

    public void setParam(FacetQueryParam param) {
        this.param = param;
    }

    public List<SolrFacetBean> getFacetFields() {
        return facetFields;
    }
}
